package com.ym.domain.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson2.JSON;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * [Sa-Token 权限认证] 配置类
 */
@Configuration
public class SaTokenConfigure {

    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter()
                .addInclude("/**")
                .addExclude(
                        "/customer/registerCustomer",
                        "/customer/login",
                        "/common/code/verifyCode",
                        //"/login/admin/login",
                        //"/sms/captcha",
                        //"/verify/code",
                        "/test/test"
                )
                .setAuth(obj -> {
                    StpUtil.checkLogin();
                    //System.out.println("---kkkkkkkkkkkkkk :");
                    //SaRouter.match("/*/admin/**").check(r -> System.out.println("--满足条件--333"));
                    SaRouter.match("/*/admin/**").check(r -> StpUtil.checkRole("admin"));
                })
                .setError(e -> {
                    SaHolder.getResponse().setHeader("Content-Type", "application/json;charset=UTF-8");
                    return JSON.toJSONString(SaResult.error("没有登录或没有操作权限"));
                });
    }

}
